package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("specification")
public class SpecificationController {

    private static final Logger logger = LoggerFactory.getLogger(SpecificationController.class);

    @Reference
    private SpecificationService specificationService;

    @RequestMapping("toSpecification")
    public String toSpecification(){

        return "admin/specification";
    }

    @RequestMapping(value = "getListSpecifications",method = RequestMethod.POST)
    @ResponseBody
    public Result<PageUtil<TbSpecification>> getListSpecifications(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                   String specName ){

        try {
            PageInfo<TbSpecification> pageInfo = specificationService.listSpecifications(pageNumber,pageSize,specName);
            PageUtil<TbSpecification> pageUtil = new PageUtil<>();
            pageUtil.setPageNumber(pageNumber);
            pageUtil.setPageSize(pageSize);
            pageUtil.setList(pageInfo.getList());
            pageUtil.setTotal(pageInfo.getTotal());
            return Result.jsonResponse(ResultEnum.OK).setData(pageUtil);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "insertSpecification",method = RequestMethod.POST)
    @ResponseBody
    public Result insertSpecification(String jsonStr,String specName){
        if (null == specName || "".equals(specName)
                || null == jsonStr || "".equals(jsonStr)){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            List<TbSpecificationOption> tbSpecificationOptions = JSON.parseArray(jsonStr, TbSpecificationOption.class);
            int i = specificationService.insertSpecificationAndTbSpecificationOption(specName,tbSpecificationOptions);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "getSpecificationById/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Result<TbSpecification> getSpecificationById(@PathVariable("id") Long id){
        if (null == id || "".equals(id) || id < 0){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            TbSpecification tbSpecification = specificationService.getSpecificationById(id);
            List<TbSpecificationOption> options = specificationService.getTbSpecificationOptionsById(id);
            tbSpecification.setOptions(options);
            return Result.jsonResponse(ResultEnum.OK).setData(tbSpecification);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "updateSpecification",method = RequestMethod.POST)
    @ResponseBody
    public Result updateSpecification(String jsonStr,String specName,Long id){
        if (null == jsonStr || "".equals(jsonStr) || null == specName
                || "".equals(specName) || null == id || "".equals(id)){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            List<TbSpecificationOption> tbSpecificationOptions = JSON.parseArray(jsonStr, TbSpecificationOption.class);
            int i = specificationService.updateSpecificationAndTbSpecificationOption(tbSpecificationOptions,specName,id);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "deleteBatch",method = RequestMethod.POST)
    @ResponseBody
    public Result deleteSpecification(@RequestParam("ids[]") Long[] ids){
        try {
            int i = specificationService.deleteSpecificationBatch(ids);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }
}
